package com.htc.dlc;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.htc.adapter.NewsAdapter;
import com.htc.dlc.fragment.FragmentHome;
import com.htc.global.GlobalUtils;
import com.htc.menu.MenuController;
import com.htc.slidingmenu.library.PageIndicator;
import com.htc.utils.CommonActions;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.htc.menu.MenuController.MenuSelectedListener;

public class MainActivity extends FragmentActivity implements MenuSelectedListener {

	MenuController menuController;
	View menuView;
	View menuViewSecondary;
	LinearLayout blurimageView;
	CommonActions ca;

	Handler handler;
	public static final int DELAY = 5000;
	ViewPager mPager;
	PageIndicator mIndicator;
	NewsAdapter adapter;
	ArrayList<Object> arrData;
	ListView lv_home;
	TextView tv_actionbar_title;
	private GlobalUtils myApplication;
	
	// Ù…Ù„ØªÙ‚Ù‰ Ø§Ù„Ù�Ù†
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		FragmentHome homeFragment = new FragmentHome();
		getSupportFragmentManager().beginTransaction()
				.add(R.id.fl, homeFragment).commit();
		
		setContentView(R.layout.activity_home);
		this.ca = new CommonActions(this);
		initViews();
		myApplication = (GlobalUtils)this.getApplicationContext();
		this.menuController = new MenuController(this.menuView,
				this.menuViewSecondary, this.blurimageView, this,
				getBaseContext(), MainActivity.this);

//		arrData = new ArrayList<Object>();
//		ImagePagerAdapter adapterimg = new ImagePagerAdapter();
//		mPager.setAdapter(adapterimg);
//
//		mIndicator.setViewPager(mPager);
//
//		handler = new Handler();
//		handler.removeCallbacks(incrementPage);
//		handler.postDelayed(incrementPage, DELAY);

//		lv_home.setOnItemClickListener(this);

//		tv_actionbar_title = (TextView) findViewById(R.id.tv_actionbar_title);
//		tv_actionbar_title.setText("Dubai Ladies Club");

//		adapter = new NewsAdapter(this, R.layout.adapter_news, arrData);
//		lv_home.setAdapter(adapter);
//		initData1();
		

	}

	private void initViews() {

//		lv_home = ((ListView) findViewById(R.id.lv_home));
//		View header = getLayoutInflater().inflate(R.layout.activity_slishow,
//				null);
//		View footer = getLayoutInflater().inflate(R.layout.activity_footer,
//				null);
//
//		mPager = (ViewPager) header.findViewById(R.id.pager);
//		lv_home.addHeaderView(header);
//		lv_home.addFooterView(footer);
		this.menuView = findViewById(R.id.menu_container);
		this.menuViewSecondary = findViewById(R.id.menu_secondary);
		this.blurimageView = (LinearLayout) findViewById(R.id.lay_blur_image);

//		mIndicator = (CirclePageIndicator) header.findViewById(R.id.indicator);
//		mPager = (ViewPager) header.findViewById(R.id.pager);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onMenuSelected(int paramInt) {
		// TODO Auto-generated method stub

	}

	private Runnable incrementPage = new Runnable() {

		@Override
		public void run() {
			int currentItem = mPager.getCurrentItem();

			int maxItems = mPager.getAdapter().getCount();

			if (maxItems != 0) {

				mPager.setCurrentItem((currentItem + 1) % maxItems, true);

			} else {

				mPager.setCurrentItem(0, true);

			}

			handler.postDelayed(incrementPage, DELAY);

		}
	};

	private class ImagePagerAdapter extends PagerAdapter {
		private int[] mImages = new int[] { R.drawable.home1, R.drawable.home2,
				R.drawable.home3, R.drawable.home4, R.drawable.home5 };

		@Override
		public int getCount() {
			return mImages.length;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			Context context = MainActivity.this;
			ImageView imageView = new ImageView(context);
			imageView.setScaleType(ImageView.ScaleType.FIT_XY);
			imageView.setImageResource(mImages[position]);

			((ViewPager) container).addView(imageView, 0);
			return imageView;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((ImageView) object);
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return (view == object);
		}

		/**
		 * Called when the a change in the shown pages has been completed. At
		 * this point you must ensure that all of the pages have actually been
		 * added or removed from the container as appropriate.
		 * 
		 * @param arg0
		 *            The containing View which is displaying this adapter's
		 *            page views.
		 */
		@Override
		public void finishUpdate(ViewGroup arg0) {
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(ViewGroup arg0) {
		}

	}

//	private void initData1() {
//
//		ActionEvent e = new ActionEvent(this, Constants.GET_NEWS_MODEL, null,
//				null);
//		NewsModelController.getInstance().handleViewEvent(e);
//	}
//
//	@SuppressWarnings("unchecked")
//	public void handleControllerViewEvent(ActionEvent e) {
//		switch (e.action) {
//		case Constants.GET_NEWS_MODEL:
//			ArrayList<NewsModuleDTO> newArr = (ArrayList<NewsModuleDTO>) e.viewData;
//			new VerySlowTask(newArr).execute();
//			break;
//		default:
//			break;
//		}
//	}
//
//	public class VerySlowTask extends AsyncTask<String, Object, Void> {
//		List<NewsModuleDTO> _newArr;
//
//		public VerySlowTask(List<NewsModuleDTO> arr) {
//			_newArr = arr;
//		}
//
//		@Override
//		protected void onPreExecute() {
//			super.onPreExecute();
//		}
//
//		@Override
//		protected Void doInBackground(String... params) {
//			for (int i = 0; i < _newArr.size(); i++) {
//				publishProgress(_newArr.get(i));
//
//			}
//			return null;
//		}
//
//		@Override
//		protected void onProgressUpdate(Object... values) {
//			arrData.add(values[0]);
//
//		}
//
//		@Override
//		protected void onPostExecute(Void result) {
//			// TODO Auto-generated method stub
//			super.onPostExecute(result);
//			adapter.notifyDataSetChanged();
//		}
//
//	}
//
//	@Override
//	public void onItemClick(AdapterView<?> parent, View view, int position,
//			long id) {
//		Object o = adapter.getArrObject().get(position - 1);
//		NewsModuleDTO dto = (NewsModuleDTO) o;
//		Intent intent = new Intent(MainActivity.this, DetailNewsActivity.class);
//		Bundle data = new Bundle();
//		data.putString("title", dto.getNewsModuleTitle());
//		data.putString("description", dto.getNewsModuleDescription());
//		Bitmap bmp = dto.getNewsBitmapImage();
//
//		ByteArrayOutputStream stream = new ByteArrayOutputStream();
//		bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
//		byte[] byteArray = stream.toByteArray();
//		intent.putExtra("image", byteArray);
//		intent.putExtras(data);
//		startActivity(intent);
//
//	}

}
